/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.servicelayer.gamelogic.resolver;

import cz.muni.fi.diplomka.servicelayer.dto.Message;
import cz.muni.fi.diplomka.servicelayer.dto.ResearchInProgress;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.ResearchManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.MessageManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.ResearchManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.MessageManagerImpl;
import java.sql.Timestamp;

/**
 * Class used for checking if Research has been finished
 *
 * @author Jakub Kutil
 */
public class ResearchResolver {

    private static ResearchManager cityResearchManager = new ResearchManagerImpl();
    private static MessageManager messageManager = new MessageManagerImpl();

    public static void main(String[] args) throws InterruptedException {
        while (true) {
            for (ResearchInProgress researchInProgress : cityResearchManager.getAllResearchesInProgress()) {
                Timestamp now = new Timestamp(System.currentTimeMillis());

                if (researchInProgress.getTimeOfFinish().before(now)) {
                    cityResearchManager.finishResearch(researchInProgress);

                    Message message = new Message();
                    message.setIdFrom(new Long(1));
                    message.setIdTo(researchInProgress.getPlayerId());
                    message.setSubject("Finished Research");
                    message.setBody("Our scholars have finished researching " + researchInProgress.getResearchType() + ".");
                    message.setTime(now);
                    message.setUnread(true);

                    messageManager.addMessage(message);
                }
            }
            Thread.sleep(1000);
        }
    }
}
